<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 5/10/24
 * Time: 10:47 AM
 */

namespace app\adminapi\logic\shield;


use app\common\logic\BaseLogic;
use app\common\model\shield\BlackIp;

class BlackIpLogic extends BaseLogic
{

    /**
     * 新增
     *
     * @param $params
     * @return bool|int
     */
    public static function add($params)
    {
        $ipAddress = $params['ip_address'] ?? '';
        if (strpos($ipAddress, ',') !== false) {
            $ipArr = explode(',', $ipAddress);
        } else {

            $ipArr = explode('，', $ipAddress);
        }

        if (empty($ipArr)) {

            return false;
        }

        $insertData = [];
        foreach ($ipArr as $ip) {
            if (BlackIp::where('ip_address', $ip)->findOrEmpty()->isEmpty()) {
                array_push($insertData, ['ip_address' => $ip, 'create_time' => time(), 'update_time' => time()]);
            }
        }

        BlackIp::insertAll($insertData);

        return true;
    }



    /**
     * 删除
     *
     * @return bool
     */
    public static function delete($id)
    {
        return BlackIp::destroy($id);
    }
}
